package com.penghaisoft.microwms.base.web.auth.controller;

import com.penghaisoft.microwms.base.web.auth.service.IBaseResourceService;
import com.penghaisoft.microwms.common.base.model.BaseResource;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.dto.BaseResourceDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资源类
 */
@RestController
@RequestMapping(value = "/resource")
@Slf4j
public class BaseResourceController extends BaseController {

	@Autowired
	private IBaseResourceService baseResourceService;//资源服务


	/**
	 * 新增记录
	 *
	 * @param baseResource
	 * @return
	 */
	@PostMapping("c")
	public TransResult create(BaseResource baseResource) {
		baseResource.preInsert();
		Resp resp = baseResourceService.create(baseResource);
		// TODO 新建资源,刷新shiro-filter
		return fromResp(resp);
	}

	/**
	 * 删除记录（逻辑删除active_flag=0)
	 *
	 * @param baseResource
	 * @return
	 */
	@PostMapping("d")
	public TransResult delete(BaseResource baseResource) {
		baseResource.preUpdate();
		Resp resp = baseResourceService.delete(baseResource);
		// TODO 删除资源 1.清除shiro-cache 2.刷新shiro-filter
		return fromResp(resp);
	}

	/**
	 * 查询列表
	 *
	 * @param page
	 * @return
	 */
	@PostMapping("list")
	public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows, BaseResource baseResource) {
		Pager<BaseResource> result = baseResourceService.findListByCondition(page, rows, baseResource);
		return success(result);
	}

	/**
	 * 查询单条
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("r/{id}")
	public TransResult findById(@PathVariable String id) {
		BaseResource baseResource = baseResourceService.queryOneWithParent(id);
		return success(baseResource);
	}

	/**
	 * 修改记录
	 *
	 * @param baseResource
	 * @return
	 */
	@PostMapping("u")
	public TransResult update(BaseResource baseResource) {
		baseResource.preUpdate();
		Resp resp = baseResourceService.update(baseResource);
		// TODO 修改资源 1.清除shiro-cache 2.刷新shiro-filter
		return fromResp(resp);
	}

	public TransResult findModalsLoginName() {
//		TODO 查询当前用户能看到的模块
		String loginName = getLoginName();// 登录账户
		return success();
	}

	/**
	 * 根据登录用户查询菜单数据（查询当前用户选择工厂下的菜单)
	 *
	 * @return
	 */
	@GetMapping("menu")
	public TransResult findMenuByLoginName() {
//		TODO 只查询base模块下的菜单
		String loginName = getLoginName();// 登录账户
		String factoryCode = getCurrentFactoryCode();// 工厂编码
//		List<BaseResource> menuList = baseResourceService.findMenuByLoginName(loginName);
		List<BaseResource> menuList = baseResourceService.findMenuByLoginNameAndFactory(loginName,factoryCode);
		return success(menuList);
	}

	/**
	 * 查询所有资源树
	 *
	 * @return
	 */
	@PostMapping("tree")
	public TransResult findAllResourceTree() {
		List<BaseResourceDto> resourceList = baseResourceService.findResourceTree();
		return success(resourceList);
	}
}
